from db import db_handler
from lib import common

#1.查看登陆用户的账户信息，及余额
def check_account_balance_interface(name):
    user_dic=db_handler.select(name)
    if user_dic:
        return True,user_dic
    else:
        return False,'该用户账号信息及余额不存在....'



#2.转账的功能接口
def transfer_interface(to_user,to_money,name):
    my_user_dic=db_handler.select(name)
    to_user_dic=db_handler.select(to_user)
    if to_user_dic:
        if my_user_dic['balance']>=to_money:
            my_user_dic['balance']-=to_money
            to_user_dic['balance']+=to_money
            db_handler.save(my_user_dic)
            db_handler.save(to_user_dic)

            common.load_my_logging_cfg(my_user_dic['role'],name).info('%s 转账给 %s ,%s元成功！' % (name, to_user, to_money))
            return True,'转账成功，您的余额为:%s'%my_user_dic['balance']

        else:
            common.load_my_logging_cfg(my_user_dic['role'], name).warning('您的余额为 %s, 不足已转账给%s %s元，请还款后再进行转账操作.'%(my_user_dic['balance'], to_user, to_money))
            return False,'您的账户余额不足，请余额充足后重试...'
    else:
        return False,'您要转账的账户:%s 不存在，请核对后重试 '



#3.取款功能:收取5%的手续费
def withdraw_interface(with_money,name):
    with_money +=(1+0.05)
    user_dic=db_handler.select(name)
    if user_dic['balance']>=with_money:
        user_dic['balance']-=with_money
        db_handler.save(user_dic)

        common.load_my_logging_cfg(user_dic['role'], name).info(
            '取款成功，您的余额为:%s 元,取款的手续费为:%s' % (user_dic['balance'], with_money * 0.05))

        return True,'取款成功,您的余额为:%s,取款手续费为:%s'%(user_dic['balance'],with_money*0.05)
    else:
        common.load_my_logging_cfg(user_dic['role'], name).warning(
            '您的余额不足为%s元，无法取款 %s元' % (user_dic['balance'],with_money))

        return False,'您的余额不足，请重试取款金额...'

#4.还款功能的接口
def repay_interface(repay_money,name):
    user_dic=db_handler.select(name)
    if user_dic['balance'] < user_dic['credit']:
        user_dic['balance']+=repay_money
        db_handler.save(user_dic)

        common.load_my_logging_cfg(user_dic['role'], name).info('还款成功，您的余额为:%s,您的信用额度为:%s' % (user_dic['balance'],user_dic['credit']))
        return True,'还款成功，当前余额为:%s'%user_dic['balance']
    else:
        common.load_my_logging_cfg(user_dic['role'], name).warning('您尚未有欠款,请勿充值到信用卡中')
        return False,'注意:尚无额外欠款，存入的金额无利息!'